<?php

namespace app\model;

use think\Model;

class SystemTargetLib extends Model
{
    /**
     * 首页目标库
     */
    protected $createTime='create_date';

    public function addDataModel($param)
    {
        return self::create ($param);
    }

    /**
     * 获取个人用户目标
     * @param $date
     * @param $uid
     * @param $field
     * @return mixed
     */
    public function getUidMoonDataModel($date, $uid,$field,$weekType=1)
    {
        return $this->where (['type'=>1,'uid'=>$uid,'week_type'=>$weekType])->whereMonth('moon_date',$date)->value ($field);
    }
    public function getUidWeekDataModel($date, $uid,$field,$weekType=1)
    {
        return $this->where (['type'=>1,'uid'=>$uid,'week_type'=>$weekType])->whereTime('moon_date',$date)->value ($field);
    }

    /**
     * 获取部门目标
     * @param $date
     * @param $oid
     * @param $field
     * @return array|null
     */
    public function getOrgMoonDataModel($date,$oid,$field,$weekType=1)
    {
        return $this->where (['type'=>2,'uid'=>$oid,'week_type'=>$weekType])->whereMonth('moon_date',$date)->value ($field);
    }
    public function getOrgWeekDataModel($date,$oid,$field,$weekType=1)
    {
        return $this->where (['type'=>2,'uid'=>$oid,'week_type'=>$weekType])->whereTime('moon_date',$date)->value ($field);
    }

    public function getOrgMoonJsonDataModel($date,$oid,$type,$weekType=1)
    {
        if (is_array ($oid)){
            $oid = $oid['id'];
        }

        $data = $this->where (['type'=>$type,'uid'=>$oid,'week_type'=>$weekType])->whereMonth('moon_date',$date)->findOrEmpty ();

        if (!$data->isEmpty ()){
            $result =  $data->toArray ();
            if ($result){
                return json_decode ($result['target_lib'],true);
            }
        }
        return [];
    }

    public function getOrgWeekJsonDataModel($date,$oid,$type,$weekType=1)
    {
        if (is_array ($oid)){
            $oid = $oid['id'];
        }

        $data = $this->where (['type'=>$type,'uid'=>$oid,'week_type'=>$weekType])->whereTime('moon_date',$date)->findOrEmpty ();

        if (!$data->isEmpty ()){
            $result =  $data->toArray ();
            if ($result){
                return json_decode ($result['target_lib'],true);
            }
        }
        return [];
    }

    /**
     * 添加目标数据
     * @param $date
     * @param $id
     * @param $type
     * @param $data
     * @return bool
     */
    public function editWhereIdTypeModel($date,$id,$type,$data,$weekType=1)
    {
        $find =  $this->where (['moon_date'=>$date,'uid'=>$id,'week_type'=>$weekType])->findOrEmpty ();

        if ($find->isEmpty ()){
            self::create($data);
        }else{
            $this->where (['moon_date'=>$date,'uid'=>$id,'week_type'=>$weekType])->update ($data);
        }
        return true;
    }

    /**
     * 获取月目标
     * @param $dates
     * @param $id
     * @param $type
     * @return array|null
     */
    public function getMoonDataModel($dates,$id,$type,$weekType=1)
    {
        $data = $this->whereTime ('moon_date',$dates)->where (['uid'=>$id,'type'=>$type,'week_type'=>$weekType])->field ('moon_day as weekday,moon_target as target')->findOrEmpty ();
        if (!$data->isEmpty ()){
            return $data->toArray ();
        }
        return null;
    }

    /**
     * 查询周目标业绩
     * @param $dates
     * @param $cicrData
     * @param $id
     * @param $type
     * @return array|null
     */
    public function getCircModel($dates,$cicrData,$id,$type,$weekType=1)
    {
        return $this->whereTime ('moon_date',$dates)->where (['uid'=>$id,'type'=>$type,'week_type'=>$weekType])->value ($cicrData);
    }
    public function getCircsModel($dates,$cicrData,$id,$type,$weekType=1)
    {
        $data = $this->whereTime ('moon_date',$dates)->where (['uid'=>$id,'type'=>$type,'week_type'=>$weekType])->findOrEmpty ();
        if(!$data->isEmpty ()){
            return $data[$cicrData];
        }
        return null;
    }

    /**
     * 获取月度目标业绩
     * @param $dates
     * @param $id
     * @param $type
     * @return mixed
     */
    public function getValueTargetModel($dates,$id,$type,$weekType=1)
    {
        return $this->whereTime ('moon_date',$dates)->where (['uid'=>$id,'type'=>$type,'week_type'=>$weekType])->value ('moon_target');
    }
    /**
     * 获取月度目标天数
     * @param $dates
     * @param $id
     * @param $type
     * @return mixed
     */
    public function getValueTargetDayModel($dates,$id,$type,$weekType=1)
    {
        return $this->whereTime ('moon_date',$dates)->where (['type'=>$type,'week_type'=>$weekType])->value ('moon_day');
    }
}